$(function () {
    init();
    $('.center').tabs({
        onSelect:function (title,index) {
            var nodeU = $(".menu-tree").tree('getSelected');
            var tab = $('.center').tabs('getSelected');  // get selected panel
            var idPanel = tab.panel('options').id;
            url = "/2";
            if(nodeU){
                if(nodeU.data){
                    $.each(nodeU.data,function (index,item) {
                        if(item.id==idPanel){
                            url = item.url;
                        }
                        $("#"+item.id).empty();
                    })
                }
                if(nodeU.url){
                    url = nodeU.url;
                }
            }
            var tab = $('.center').tabs('getSelected');  // get selected panel
            $('.center').tabs('update', {
                tab: tab,
                options: {
                    href: url // the new content URL
                }
            });
        }
    });
});

function nodeClick(node) {
    var tabs = $('.center').tabs("tabs");
    $.each(tabs,function (index,item) {
        $('.center').tabs("close",0);
    })
    if (node) {
        if (node.url) {
            $('.center').tabs("add",{
                id: node.id,
                title: node.text,
                href: node.url
            });
        }
        if (node.data) {
            $.each(node.data,function (index,item) {
                $('.center').tabs("add",{
                    id:item.id,
                    title: item.name,
                    selected:index==0,
                    href: node.url
                });
            });

        }
    }
}

function init() {
    $(".menu-tree").tree({
        data:[
            {
                id:'1',
                text:'系统管理',
                state: 'open',
                children:[
                    {
                        id:'1-1',
                        text:'2019',
                        state: 'open',
                        children: [
                            {
                                id:'1-1-1',
                                text:'1-1-1',
                                state: 'open',
                                data:[
                                    {
                                        id:'x-1',
                                        name:'x-1',
                                        url:"/1"
                                    },
                                    {
                                        id:'x-2',
                                        name:'x-2',
                                        url:"/1"
                                    },
                                    {
                                        id:'x-3',
                                        name:'x-3',
                                        url:"/1"
                                    }
                                ]
                            },
                            {
                                id:'1-1-2',
                                text:'1-1-2',
                                state: 'open',
                                data:[
                                    {
                                        id:'y-1',
                                        name:'y-1',
                                        url:'/1'
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        id:'1-2',
                        text:'2020',
                        state: 'open',
                        children: [
                            {
                                id:'1-2-1',
                                text:'1-2-1',
                                checked:true,
                                state: 'open',
                                url:"/1"
                            },
                            {
                                id:'1-2-2',
                                text:'1-2-2',
                                state: 'open',
                                url:"/1"
                            },
                            {
                                id:'1-2-3',
                                text:'1-2-3',
                                state: 'open',
                                url:"/2"
                            }
                        ]
                    }
                ]

            },
        ],
        onClick:function (node){
            nodeClick(node);
        }
    });
}